package com.xjw.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.junit.Test;

/**      
 * 项目名称：springMvc  
 * 实现功能：  
 * 类名称：LuceneTest   
 * 类描述：(该类的主要功能)
 * 创建人：徐纪伟 
 * E-mail: xujiwei558@126.com
 * 创建时间：Feb 8, 2014 3:56:39 PM   
 * 修改人：   
 * 修改时间：   
 * 修改备注：   
 * @version    
 */
public class Indexer {
	
	private String fieldName;//索引段名称
	private String endStr; //问价后缀名称
	
	public Indexer(){
		
	}
	 
	public Indexer(String fieldName, String endStr){ 
		this.fieldName= fieldName; 
		this.endStr= endStr;  
	} 

	
	
	@Test
	public void main(){
        
        Indexer indexer = new Indexer("filepath",".txt");
		try {
			 /**/ /*  指明要索引文件夹的位置*/   
	        File fileDir = new File("F:\\开发\\java\\lucene\\data");   
	        /**/ /*  这里放索引文件的位置  */   
	        File indexDir = new File("F:\\开发\\java\\lucene\\index"); 
			int result = indexer.createIndex(indexDir, fileDir);
			System.out.println("indexing "+result+" files.");
		} catch (IOException e) {
			e.printStackTrace();
		}

      
    }    
	
	/*
	 * 创建索引
	 */
	public int createIndex(File indexDir, File fileDir) throws IOException{
		//Analyzer luceneAnalyzer =  new StandardAnalyzer(Version.LUCENE_46);
		Analyzer luceneAnalyzer =  new StandardAnalyzer(Version.LUCENE_46);
	        IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_46,luceneAnalyzer);
	        Directory directory = FSDirectory.open(indexDir);
	        IndexWriter indexWriter =  new IndexWriter(directory,indexWriterConfig);
//	        if (IndexWriter.isLocked(directory)) {
//				IndexWriter.unlock(directory);
//			}
	        indexWriter.deleteAll();
	        indexDirectory(indexWriter, fileDir); 
	        int numIndexed = indexWriter.numDocs(); 
	        indexWriter.close(); 

	        return numIndexed;

	}
	
	public void indexDirectory(IndexWriter writer,File dir) throws IOException{
		File[] files = dir.listFiles();
		for (File file : files) {
			if(file.isDirectory()){
				indexDirectory(writer, file);
			}else if(file.getName().endsWith(this.endStr)){
				indexFile(writer, file);
			}
		}	
	}
	
	/**
	 * 对文件创建索引
	 * @param writer
	 * @param f
	 * @throws IOException
	 * @author lm
	 */
	@SuppressWarnings("deprecation")
	public void indexFile(IndexWriter writer,File f) throws IOException{
		// 过滤隐藏文件，不存在文件，不可读文件
		if(f.isHidden() || !f.exists() || !f.canRead()) 
			return;
		System.out.println("Indexing: "+f.getCanonicalPath());
		
		Document doc = new Document();
		String content = getTXT(f,"GBK");
		doc.add(new Field("content", content, Field.Store.YES, Field.Index.ANALYZED));
		doc.add(new Field(this.fieldName, f.getCanonicalPath(), Field.Store.YES, Field.Index.ANALYZED));
		writer.addDocument(doc);
	}
	
	/**
	 * 读取文件
	 * @param file
	 * @param charset
	 * @return
	 * @throws IOException
	 * @author lm
	 */
	public String getTXT(File file,String charset) throws IOException{
		FileInputStream fileInputStream = new FileInputStream(file);
		InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream,charset);
		BufferedReader reader = new BufferedReader(inputStreamReader);
		String line = new String();
		String result = new String();
		while( (line=reader.readLine())!=null ){
			result += line;
		}
		reader.close();
		return result;
	}

	
	
	
	

}
